<?php
if (!isset($_SESSION['logged']))
    print "<script language='javascript'>
        window.location.href='./?url=404';
        </script>";
else {
?>
<div id="mainCol" class="clearfix">
    <h3 id="why">All files...</h3>
    <ul id="maincon">
        <li class="clearfix last">
            <table width="100%">
            <?
            $iduser = $_SESSION['id'];
            $sql = "SELECT `f`.*, `t`.`title` as `type`
                    FROM `file` `f`, `user_file` `uf`, `type` `t`
                    WHERE `uf`.`file_id` = `f`.`id`
                        AND `f`.`type_id` = `t`.`id`
                    GROUP BY `f`.`id`
                    ORDER BY `f`.`id` DESC;";
            $result = mysql_query($sql, $link);
            if(mysql_num_rows($result)==0){
                ?>
                <tr><td><h4>Nothing here.</h4></td></tr>
                <?
            }else{
                ?>
                    <tr>
                        <td width="32px"></td>
                        <td>
                        </td>
                        <td width="32px"></td>
                        <td width="32px"></td>
                    </tr>
                <?
                while($row = mysql_fetch_assoc($result)){
                    $idfile = $row['id'];
                    $filetype = strtolower($row['type']);

                    $sqlowner = "SELECT *
                                 FROM `user_file`
                                 WHERE `user_id` = '$iduser'
                                     AND `file_id` = '$idfile';";
                    $resultowner = mysql_query($sqlowner, $link);
                    $owner = mysql_num_rows($resultowner);

                    ?>
                    <tr>
                        <td>
                            <a href="?url=file&file=<?= $idfile; ?>">
                                <img src="images/<?= $filetype; ?>.png" title="<?= $row['title']; ?>" width="24px" height="24px" alt="Type" class="image" />
                            </a>
                        </td>
                        <td>
                            <a href="?url=file&file=<?= $idfile; ?>"><b><?= $row['title']; ?></b></a>
                        </td>
<?
                    if ($owner == 1) {
?>
                        <td align="right">
                            <a href="?url=fileedit&file=<?= $idfile; ?>" title="Edit">
                                <img src="images/edit.png" width="24px" height="24px" alt="Edit" class="image" />
                            </a>
                        </td>
                        <td align="right">
                            <a href="?url=filedeleting&file=<?= $idfile; ?>" onclick="return confirm('Are you sure you want to delete this item?');" title="Delete">
                                <img src="images/delete.png" width="24px" height="24px" alt="Delete" class="image" />
                            </a>
                        </td>
<?
                    } else {
?>
                        <td></td>
                        <td align="right">
                            <a href="?url=fileadding&file=<?= $row['id']; ?>" title="I have this file...">
                                <img src="images/add.png" width="24px" height="24px" alt="Add" class="image" />
                            </a>
                        </td>
<?
                    }
?>
                    </tr>
                    <?
                }
            }
            ?>
            </table>
        </li>
    </ul>
</div>
<?
}
?>
